<?php
declare (strict_types=1);

namespace app\api\controller;

use think\response\Json;
use app\api\model\User as UserModel;
use app\common\model\user\Grade as GradeModel;
use think\facade\Db;


class User extends Controller
{
    public function index(bool $update_time=false)
    {
        if($this->user != false){
            $data = [
                'is_login' => true,
                'avatar' => $this->user['avatar'],
                'nick_name' => $this->user['nick_name'],
                'gender' => $this->user['gender'],
                'country' => $this->user['country'],
                'province' => $this->user['province'],
                'city' => $this->user['city'],
                'address_id' => $this->user['address_id'],
                'balance' => $this->user['balance'],
                'points' => $this->user['points'],
                'pay_money' => $this->user['pay_money'],
                'expend_money' => $this->user['expend_money'],
                'grade_id' => $this->user['grade_id'],
                'platform' => $this->user['platform'],
                'status' => $this->user['status'],
                'last_login_time' => $this->user['last_login_time'],
                'create_time' => $this->user['create_time'],
                'update_time' => $this->user['update_time']
            ];
            if($update_time){
                UserModel::update([
                    'update_time' => time()
                ],['user_id' => $this->user['user_id']]);
                $data['token'] = UserModel::login($this->user['user_id']);
            }
        }else{
            $data = [
                'is_login' => false,
                'nick_name' => '',
                'avatar' => '',
                'gender' => 0,
                'country' => '',
                'province' => '',
                'city' => '',
                'address_id' => '',
                'balance' => 0,
                'points' => 0,
                'pay_money' => 0,
                'expend_money' => 0,
                'grade_id' => 0,
                'platform' => '',
                'status' => 1,
                'last_login_time' => 0,
                'create_time' => 0,
                'update_time' => 0
            ];
        }
        return $this->renderSuccess($data);
    }

    public function detail(): Json
    {
        $this->checkUser();
        $data = Db::name('user')->where('user_id', $this->user['user_id'])->find();
        $data['avatar_file'] = get_file_url($data['avatar']);
        unset($data['user_id']);
        unset($data['password']);
        return $this->renderSuccess($data);
    }

    public function edit()
    {
        // 用户详情
        $model = UserModel::detail($this->user['user_id']);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    private function calculateDays($timestamp1, $timestamp2) {
        $date1 = new \DateTime();
        $date1->setTimestamp($timestamp1);
        $date2 = new \DateTime();
        $date2->setTimestamp($timestamp2);
        $interval = $date1->diff($date2);
        return $interval->format('%a');
    }

    public function grade()
    {
        $list = GradeModel::where('status', 1)
            ->order(['sort', 'grade_id'])
            ->select()->toArray();
        return $this->renderSuccess($list);
    }
}
